package com.shopcart.action;

import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.shopcart.domain.Store;
import com.shopcart.domain.StoreGoodsCategory;
import com.shopcart.service.StoreGoodsCategoryService;

@Controller
@RequestMapping("storeGoodsCategory")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class StoreGoodsCategoryController extends CoreAdminController{
	@Autowired
	private StoreGoodsCategoryService storeGoodsCategoryService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String list(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String storeId,HttpServletRequest request){
		
		if("store".equals(getSessionParameter("ROLE_CODE", session))){
			storeId = getSessionParameter("id", session);
		}
		Page<StoreGoodsCategory> list = storeGoodsCategoryService.findByStoreId(storeId,new PageRequest(start/length, length, new Sort(Direction.DESC,"sort")));
		
		return JSONUtils.serialize(new DataTable(draw+1,Integer.valueOf(list.getTotalElements()+""), list.getContent()));
	}
	
	@RequestMapping("/listAll")
	@ResponseBody
	public String listAll(HttpSession session,String storeId,HttpServletRequest request){
		if("store".equals(getSessionParameter("ROLE_CODE", session))){
			storeId = getSessionParameter("id", session);
		}
		Collection<StoreGoodsCategory> list = storeGoodsCategoryService.findByStoreId(storeId,new Sort(Direction.DESC,"sort"));
		return JSONUtils.serialize(list);
	}
 
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id,HttpServletRequest request){
		StoreGoodsCategory s = storeGoodsCategoryService.findById(id);
		return JSONUtils.serialize(s);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(StoreGoodsCategory goods,HttpSession session,HttpServletRequest req) throws Exception{
		if (StringUtils.isBlank(goods.getId())){
			Store s = (Store) getCurrentUser(req);
			
			goods.setId(StringUtil.getUUID());
			goods.setCreateTime(new Date());
			goods.setStoreId(s.getId());
			goods.setType(1);
			storeGoodsCategoryService.save(goods);
    	}else{
    		goods.setType(null);
    		goods.setStoreId(null);
    		storeGoodsCategoryService.updateByPrimaryKeySelective(goods);
    	}
       	return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id,HttpServletRequest request){
		String[] ids = id.split(",");
		Store s = (Store) getCurrentUser(request);
		for (String string : ids) {
			storeGoodsCategoryService.delWithGoods(s.getId(),string);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
}
